package com.qf.controller;

import com.qf.entity.Admini;
import com.qf.service.HospService;
import com.qf.util.R;
import com.qf.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/hosp")
public class HospController {
    @Autowired
    private HospService hospService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping ("/login")
    public R login(@RequestBody Admini admini){
        System.out.println(admini);
        Admini user = hospService.login(admini);
        if (user == null){
            return R.fail();
        }
        //生成token
        String token = UUID.randomUUID().toString();
        //存token
        redisUtil.setCacheObject("user:"+token,user,10, TimeUnit.MINUTES);
        redisUtil.getCacheObject("user:"+token);
        HashMap<String, Object> map = new HashMap<>();
        //调用
        map.put("token",token);

        return R.ok(map);

    }
    @GetMapping("/info")
    public R getinfo(){
        //根据令牌去redis拿令牌
        //将用户信息传给前端
        Admini admini =hospService.getInfo();
        if (admini!=null){
            return R.ok(admini);
        }
        return R.fail();
    }
    @PostMapping("/loginout")
    public R loginOut(){
        redisUtil.deleteObject("token:"+"token");
        return R.ok();
    }

}
